<template>
  <div class="app-container">
    <el-button

      class="filter-item marginRB10"
      type="primary"
      icon="el-icon-plus"
      @click="handleCreate()"
      >添加</el-button
    >
    <el-button class="marginRB10" type="danger" @click="allDel()" plain
      >批量删除</el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      style="margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id"> </el-table-column>
      <el-table-column prop="grade" label="等级"> </el-table-column>
      <el-table-column prop="experience" label="经验值"> </el-table-column>
      <el-table-column prop="head_picture" label="图片">
        <template slot-scope="scope">
          <el-popover placement="right" width="100" trigger="hover">
            <img
              :src="scope.row.head_picture"
              alt=""
              style="max-width: 400px; max-height: 400px"
            />
            <img
              slot="reference"
              :src="scope.row.head_picture"
              alt=""
              style="width: 50px; height: 25px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column min-width="180" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            plain
            >编辑</el-button
          >
          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="等级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入等级" />
        </el-form-item>
        <el-form-item label="经验值" prop="experience">
          <el-input v-model="form.experience" placeholder="请输入经验值" />
        </el-form-item>
        <el-form-item label="爵位图片" prop="head_picture">
          <span>{{ form.head_picture ? form.head_picture.name : "" }}</span>
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  saveData,
  deleteData,
  updateData,
  listData,
} from "@/api/statistics/statistic";
export default {
  data() {
    return {
      confirmBtn: false,

      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
      },
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: "",
      titleMap: {
        update: "编辑",
        create: "增加",
      },
      form: {
        id: undefined,
        grade: undefined,
        experience: undefined,
        head_picture: undefined,
      },
      rules: {
        grade: [{ required: true, message: "请输入等级", trigger: "blur" }],
        experience: [
          { required: true, message: "请输入经验值", trigger: "blur" },
        ],
        head_picture: [
          { required: true, message: "请输入上传爵位图片", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {},
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;


      this.form.head_picture = file;
      // return isJPG && isLt2M;
    },
    getList() {
      this.listLoading = true;
      listData().then((response) => {
        this.menuList = response.data;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    handleCreate(row) {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      this.form = [];
      this.form.id = row.id;
      this.form.grade = row.grade;
      this.form.experience = row.experience;
      this.form.head_picture = row.head_picture;

      this.dialogStatus = "update";
      this.dialogFormVisible = true;

      this.form = Object.assign({}, this.form);
    },
    handleDelete(row) {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      let data = { ids: row.id };
      deleteData(data).then((response) => {
        if (response.code === 200) {
          this.getList();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000,
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    allDel() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;

      let delNum = [];
      this.$refs.dataList.selection.forEach((k) => {
        delNum.push(k.id);
      });
      if (delNum.length === 0) {
        return;
      }
      let data = { ids: delNum.toString() };
      deleteData(data).then((response) => {
        if (response.code === 200) {
          this.getList();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000,
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    submitForm() {
      if (this.confirmBtn) return;
      this.$refs["dataForm"].validate((valid) => {
        if (valid && this.dialogStatus === "create") {
          this.confirmBtn = true;
          let newForm = {
            grade: this.form.grade,
            experience: this.form.experience,
            head_picture: this.form.head_picture,
          };
          let formData = new FormData();
          for (let item in newForm) {
            newForm[item] === undefined
              ? ""
              : formData.append(item, newForm[item]);
          }
          saveData(formData).then((response) => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000,
              });
              this.dialogFormVisible = false;
            } else {
              this.confirmBtn = false;
            }
          });
        } else if (valid && this.dialogStatus === "update") {
          this.confirmBtn = true;
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          updateData(this.form).then((response) => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000,
              });
              this.dialogFormVisible = false;
            } else {
              this.confirmBtn = false;
            }
          });
        }
      });
    },
    resetForm() {
      this.form = {
        id: undefined,
        grade: undefined,
        experience: undefined,
        head_picture: undefined,
      };
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate(); // 清除整个表单的校验
      }
    },
  },
};
</script>
